home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / TickerArea.java < prev    next >
Text File  |  1998-09-15  |  4KB  |  127 lines

  1. /*
  2.  * @(#)TickerArea.java    1.5 98/03/18
  3.  *
  4.  * Copyright (c) 1996, 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.Graphics;
  32. import java.awt.Color;
  33. import java.awt.Font;
  34. import java.awt.FontMetrics;
  35. import java.util.StringTokenizer;
  36.  
  37. /**
  38.  * This ImageArea renders a string of text that constantly scrolls across
  39.  * the indicated area of the ImageMap in the specified color.
  40.  *
  41.  * @author    Jim Graham
  42.  * @version    1.5, 03/18/98
  43.  */
  44. class TickerArea extends ImageMapArea {
  45.  
  46.     String tickertext;
  47.     Color  tickercolor;
  48.     Font   tickerfont;
  49.     int    speed;        // In pixels per second for scrolling
  50.  
  51.     int tickerx;
  52.     int tickery;
  53.     int tickerlen;
  54.     long lasttick;
  55.  
  56.     public void handleArg(String s) {
  57.     StringTokenizer st = new StringTokenizer(s, ",");
  58.  
  59.     tickertext = st.nextToken();
  60.     tickercolor = Color.black;
  61.     speed = 100;
  62.     String fontname = "TimesRoman";
  63.  
  64.     if (st.hasMoreTokens()) {
  65.         fontname = st.nextToken();
  66.         if (st.hasMoreTokens()) {
  67.         String str = st.nextToken();
  68.         if (str.startsWith("#")) {
  69.             str = str.substring(1);
  70.         }
  71.         try {
  72.             int colorval = Integer.parseInt(str, 16);
  73.             tickercolor = new Color((colorval >> 16) & 0xff,
  74.                         (colorval >> 8) & 0xff,
  75.                         (colorval >> 0) & 0xff);
  76.         } catch (Exception e) {
  77.             tickercolor = Color.black;
  78.         }
  79.         if (st.hasMoreTokens()) {
  80.             str = st.nextToken();
  81.             try {
  82.             speed = Integer.parseInt(str);
  83.             } catch (Exception e) {
  84.             speed = 100;
  85.             }
  86.         }
  87.         }
  88.     }
  89.  
  90.     FontMetrics fm;
  91.     int size;
  92.     int nextsize = H;
  93.     do {
  94.         size = nextsize;
  95.         tickerfont = new Font(fontname, Font.PLAIN, size);
  96.         fm = parent.getFontMetrics(tickerfont);
  97.         nextsize = (size * 9) / 10;
  98.     } while (fm.getHeight() > H && size > 0);
  99.     tickerlen = fm.stringWidth(tickertext);
  100.     tickery = fm.getAscent();
  101.     }
  102.  
  103.     public void getMedia() {
  104.     tickerx = 0;
  105.     repaint();
  106.     lasttick = System.currentTimeMillis();
  107.     }
  108.  
  109.     public boolean animate() {
  110.     long curtick = System.currentTimeMillis();
  111.     tickerx -= ((speed * (curtick - lasttick)) / 1000);
  112.     if (tickerx > W || tickerx + tickerlen < 0) {
  113.         tickerx = W;
  114.     }
  115.     repaint();
  116.     lasttick = curtick;
  117.     return true;
  118.     }
  119.  
  120.     public void highlight(Graphics g) {
  121.     g.setColor(tickercolor);
  122.     g.setFont(tickerfont);
  123.     g.drawString(tickertext, X+tickerx, Y+tickery);
  124.     }
  125. }
  126.  
  127.